package api

import (
	"gateway/basic/config"
	order "gateway/basic/proto/orderproto"
	"gateway/handler/request"
	"gateway/handler/response"
	"github.com/gin-gonic/gin"
)

// TransitionOrderState 转换订单状态
// @Summary 转换订单状态
// @Description 使用状态机转换订单状态
// @Tags 状态机管理
// @Accept json
// @Produce json
// @Param request body request.TransitionOrderStateRequest true "状态转换请求"
// @Success 200 {object} response.Response "状态转换成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务器内部错误"
// @Router /v1/state-machine/transition [post]
func TransitionOrderState(c *gin.Context) {
	var req request.TransitionOrderStateRequest
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "请求参数错误: "+err.Error(), err.Error())
		return
	}

	// 调用RPC服务
	rpcReq := &order.TransitionOrderStateReq{
		OrderId:      req.OrderID,
		Event:        req.Event,
		OperatorId:   req.OperatorID,
		OperatorType: req.OperatorType,
		Reason:       req.Reason,
	}

	rpcResp, err := config.OrderClient.TransitionOrderState(c.Request.Context(), rpcReq)
	if err != nil {
		response.SrvError(c, "状态转换失败: "+err.Error(), err.Error())
		return
	}

	if rpcResp.Code != 200 {
		response.SrvError(c, rpcResp.Message, nil)
		return
	}

	response.Success(c, rpcResp.Message, map[string]interface{}{
		"order_id":   req.OrderID,
		"from_state": rpcResp.FromState,
		"to_state":   rpcResp.ToState,
		"event":      rpcResp.Event,
		"timestamp":  rpcResp.Timestamp,
	})
}

// GetOrderStateInfo 获取订单状态信息
// @Summary 获取订单状态信息
// @Description 获取订单当前状态和可执行的事件
// @Tags 状态机管理
// @Accept json
// @Produce json
// @Param order_id query uint64 true "订单ID"
// @Success 200 {object} response.Response "获取状态信息成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务器内部错误"
// @Router /v1/state-machine/state-info [get]
func GetOrderStateInfo(c *gin.Context) {
	var req request.GetOrderStateInfoRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "请求参数错误: "+err.Error(), err.Error())
		return
	}

	// 调用RPC服务
	rpcReq := &order.GetOrderStateInfoReq{
		OrderId: req.OrderID,
	}

	rpcResp, err := config.OrderClient.GetOrderStateInfo(c.Request.Context(), rpcReq)
	if err != nil {
		response.SrvError(c, "获取状态信息失败: "+err.Error(), err.Error())
		return
	}

	if rpcResp.Code != 200 {
		response.SrvError(c, rpcResp.Message, nil)
		return
	}

	response.Success(c, rpcResp.Message, map[string]interface{}{
		"order_id":      req.OrderID,
		"current_state": rpcResp.CurrentState,
		"state_name":    rpcResp.StateName,
		"valid_events":  rpcResp.ValidEvents,
	})
}

// GetStateMachineDiagram 获取状态机流程图
// @Summary 获取状态机流程图
// @Description 获取订单状态机的流程图数据
// @Tags 状态机管理
// @Accept json
// @Produce json
// @Success 200 {object} response.Response "获取流程图成功"
// @Failure 500 {object} response.Response "服务器内部错误"
// @Router /v1/state-machine/diagram [get]
func GetStateMachineDiagram(c *gin.Context) {
	// 调用RPC服务
	rpcReq := &order.GetStateMachineDiagramReq{}

	rpcResp, err := config.OrderClient.GetStateMachineDiagram(c.Request.Context(), rpcReq)
	if err != nil {
		response.SrvError(c, "获取流程图失败: "+err.Error(), err.Error())
		return
	}

	if rpcResp.Code != 200 {
		response.SrvError(c, rpcResp.Message, nil)
		return
	}

	response.Success(c, rpcResp.Message, map[string]interface{}{
		"diagram": rpcResp.Diagram,
	})
}

// ValidateTransition 验证状态转换
// @Summary 验证状态转换
// @Description 验证订单状态转换是否有效
// @Tags 状态机管理
// @Accept json
// @Produce json
// @Param request body request.ValidateTransitionRequest true "验证转换请求"
// @Success 200 {object} response.Response "验证结果"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务器内部错误"
// @Router /v1/state-machine/validate [post]
func ValidateTransition(c *gin.Context) {
	var req request.ValidateTransitionRequest
	if err := c.ShouldBind(&req); err != nil {
		response.ParameterError(c, "请求参数错误: "+err.Error(), err.Error())
		return
	}

	// 调用RPC服务
	rpcReq := &order.ValidateTransitionReq{
		OrderId: req.OrderID,
		Event:   req.Event,
	}

	rpcResp, err := config.OrderClient.ValidateTransition(c.Request.Context(), rpcReq)
	if err != nil {
		response.SrvError(c, "验证转换失败: "+err.Error(), err.Error())
		return
	}

	response.Success(c, rpcResp.Message, map[string]interface{}{
		"order_id":        req.OrderID,
		"event":           req.Event,
		"can_transition":  rpcResp.CanTransition,
		"from_state":      rpcResp.FromState,
		"to_state":        rpcResp.ToState,
		"from_state_name": rpcResp.FromStateName,
		"to_state_name":   rpcResp.ToStateName,
	})
}

// GetStateHistory 获取状态历史
// @Summary 获取状态历史
// @Description 获取订单的状态变更历史
// @Tags 状态机管理
// @Accept json
// @Produce json
// @Param order_id query uint64 true "订单ID"
// @Success 200 {object} response.Response "获取状态历史成功"
// @Failure 400 {object} response.Response "请求参数错误"
// @Failure 500 {object} response.Response "服务器内部错误"
// @Router /v1/state-machine/history [get]
func GetStateHistory(c *gin.Context) {
	var req request.GetStateHistoryRequest
	if err := c.ShouldBindQuery(&req); err != nil {
		response.ParameterError(c, "请求参数错误: "+err.Error(), err.Error())
		return
	}

	// 调用RPC服务
	rpcReq := &order.GetStateHistoryReq{
		OrderId: req.OrderID,
	}

	rpcResp, err := config.OrderClient.GetStateHistory(c.Request.Context(), rpcReq)
	if err != nil {
		response.SrvError(c, "获取状态历史失败: "+err.Error(), err.Error())
		return
	}

	if rpcResp.Code != 200 {
		response.SrvError(c, rpcResp.Message, nil)
		return
	}

	response.Success(c, rpcResp.Message, map[string]interface{}{
		"order_id": req.OrderID,
		"history":  rpcResp.History,
	})
}
